home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / time / test_time.c < prev    next >
C/C++ Source or Header  |  1992-05-22  |  3KB  |  117 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23.  
  24.  
  25. int
  26. DEFUN(main, (argc, argv), int argc AND char **argv)
  27. {
  28.   time_t t;
  29.   register struct tm *tp;
  30.   struct tm tbuf;
  31.   int lose = 0;
  32.  
  33.   --argc;
  34.   ++argv;
  35.  
  36.   do
  37.     {
  38.       char buf[BUFSIZ];
  39.       if (argc > 0)
  40.     {
  41.       static char buf[BUFSIZ];
  42.       sprintf(buf, "TZ=%s", *argv);
  43.       if (putenv(buf))
  44.         {
  45.           puts("putenv failed.");
  46.           lose = 1;
  47.         }
  48.       else
  49.         puts (buf);
  50.     }
  51.       tzset();
  52.       tbuf.tm_year = 72;
  53.       tbuf.tm_mon = 0;
  54.       tbuf.tm_mday = 31;
  55.       tbuf.tm_hour = 6;
  56.       tbuf.tm_min = 14;
  57.       tbuf.tm_sec = 50;
  58.     doit:;
  59.       t = mktime(&tbuf);
  60.       if (t == (time_t) -1)
  61.     {
  62.       puts("mktime() failed?");
  63.       lose = 1;
  64.     }
  65.       tp = localtime(&t);
  66.       if (tp == NULL)
  67.     {
  68.       puts("localtime() failed.");
  69.       lose = 1;
  70.     }
  71.       else if (strftime(buf, sizeof(buf), "%a %b %d %X %Z %Y", tp) == 0)
  72.     {
  73.       puts("strftime() failed.");
  74.       lose = 1;
  75.     }
  76.       else
  77.     puts(buf);
  78.       if (tbuf.tm_year == 101)
  79.     {
  80.       tbuf.tm_year = 97;
  81.       tbuf.tm_mon = 0;
  82.       goto doit;
  83.     }
  84.       ++argv;
  85.     } while (--argc > 0);
  86.  
  87.   {
  88. #define    SIZE    256
  89.     char buffer[SIZE];
  90.     time_t curtime;
  91.     struct tm *loctime;
  92.  
  93.     curtime = time (NULL);
  94.  
  95.     loctime = localtime (&curtime);
  96.  
  97.     fputs (asctime (loctime), stdout);
  98.  
  99.     strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
  100.     fputs (buffer, stdout);
  101.     strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
  102.     fputs (buffer, stdout);
  103.  
  104.     loctime->tm_year = 72;
  105.     loctime->tm_mon = 8;
  106.     loctime->tm_mday = 12;
  107.     loctime->tm_hour = 20;
  108.     loctime->tm_min = 49;
  109.     loctime->tm_sec = 05;
  110.     curtime = mktime (loctime);
  111.     strftime (buffer, SIZE, "%D %T was %w the %jth.\n", loctime);
  112.     fputs (buffer, stdout);
  113.   }
  114.  
  115.   return (lose ? EXIT_FAILURE : EXIT_SUCCESS);
  116. }
  117.